//
//  WCProjectOverviewPopUpButton.m
//  WabbitCode
//
//  Created by William Towe on 4/3/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCProjectOverviewPopUpButton.h"
#import "WCBuildTargetsGroup.h"
#import "WCBuildTarget.h"
#import "WCBuildConfiguration.h"
#import "WCProject.h"


@interface WCProjectOverviewPopUpButton (Private)
- (void)_rebuildMenu:(NSMenu *)menu;
@end

@implementation WCProjectOverviewPopUpButton
- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[super dealloc];
}

- (void)menuNeedsUpdate:(NSMenu *)menu {
	[self _rebuildMenu:menu];
}

@synthesize project=_project;

- (id)initWithProject:(WCProject *)project; {
	if (!(self = [super initWithFrame:NSMakeRect(0.0, 0.0, 125.0, 22.0) pullsDown:YES]))
		return nil;
	
	[[self cell] setArrowPosition:NSPopUpArrowAtBottom];
	[[self cell] setPreferredEdge:NSMaxYEdge];
	[[self cell] setBezelStyle:NSTexturedRoundedBezelStyle];
	[self setAutoresizingMask:NSViewWidthSizable];
	[self setAutoenablesItems:NO];
	
	_project = project;
	
	[[self menu] setDelegate:self];
	
	[self _rebuildMenu:[self menu]];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notificationHandler:) name:kWCProjectActiveBuildTargetDidChangeNotification object:_project];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notificationHandler:) name:kWCProjectActiveBuildConfigurationDidChangeNotification object:_project];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notificationHandler:) name:kWCBTBuildTargetNameDidChangeNotification object:nil];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notificationHandler:) name:kWCBCBuildConfigurationNameDidChangeNotification object:nil];
	
	return self;
}

- (void)_rebuildMenu:(NSMenu *)menu; {
	[self removeAllItems];
	
	// add the title item
	[menu addItemWithTitle:[NSString stringWithFormat:@"%@ | %@", [[[self project] activeBuildTarget] name], [[[self project] activeBuildConfiguration] name]] action:NULL keyEquivalent:@""];
	
	// add the header item for build targets
	[menu addItemWithTitle:NSLocalizedString(@"Active Build Target",@"Active Build Target") action:NULL keyEquivalent:@""];
	[[[menu itemArray] lastObject] setEnabled:NO];
	
	// add the build targets for our project
	WCBuildTarget *activeTarget = [[self project] activeBuildTarget];
	NSUInteger targetIndex = 0;
	for (WCBuildTarget *target in [[[self project] buildTargetsGroup] childNodes]) {
		NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:[target name] action:@selector(_changeActiveBuildTargetAction:) keyEquivalent:@""] autorelease];
		[item setImage:[target icon]];
		[item setTag:targetIndex++];
		[item setIndentationLevel:1];
		[item setState:(target == activeTarget)?NSOnState:NSOffState];
		[item setTarget:self];
		[menu addItem:item];
	}
	
	[menu addItem:[NSMenuItem separatorItem]];
	
	// add the header item for build configurations
	[menu addItemWithTitle:NSLocalizedString(@"Active Build Configuration",@"Active Build Configuration") action:NULL keyEquivalent:@""];
	[[[menu itemArray] lastObject] setEnabled:NO];
	
	// add the build configurations for the active build target
	WCBuildConfiguration *activeConfig = [[self project] activeBuildConfiguration];
	NSUInteger configIndex = 0;
	for (WCBuildConfiguration *config in [activeTarget buildConfigurations]) {
		NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:[config name] action:@selector(_changeActiveBuildConfigurationAction:) keyEquivalent:@""] autorelease];
		[item setImage:[config icon]];
		[item setIndentationLevel:1];
		[item setTag:configIndex++];
		[item setState:(config == activeConfig)?NSOnState:NSOffState];
		[item setTarget:self];
		[menu addItem:item];
	}
}

- (IBAction)_changeActiveBuildTargetAction:(id)sender; {
	[[self project] setActiveBuildTarget:[[[[self project] buildTargetsGroup] childNodes] objectAtIndex:[sender tag]]];
}

- (IBAction)_changeActiveBuildConfigurationAction:(id)sender; {
	[[self project] setActiveBuildConfiguration:[[[[self project] activeBuildTarget] buildConfigurations] objectAtIndex:[sender tag]]];
}

- (void)_notificationHandler:(NSNotification *)note {
	if ([[note name] isEqualToString:kWCBTBuildTargetNameDidChangeNotification]) {
		[[[[self menu] itemArray] firstObject] setTitle:[NSString stringWithFormat:@"%@ | %@", [[[self project] activeBuildTarget] name], [[[self project] activeBuildConfiguration] name]]];
	}
	else if ([[note name] isEqualToString:kWCBCBuildConfigurationNameDidChangeNotification]) {
		[[[[self menu] itemArray] firstObject] setTitle:[NSString stringWithFormat:@"%@ | %@", [[[self project] activeBuildTarget] name], [[[self project] activeBuildConfiguration] name]]];
	}
	else {
		[self _rebuildMenu:[self menu]];
	}
}
@end
